﻿
Imports System.Data
Imports System.Collections.Generic

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Moca
Imports Moca.Db
Imports Moca.Db.CommandWrapper


Namespace Db.CommandWrapper

	'''<summary>
	'''SelectCommandWrapperTest のテスト クラスです。すべての
	'''SelectCommandWrapperTest 単体テストをここに含めます
	'''</summary>
	<TestClass()> _
	Public Class SelectCommandWrapperTest
		Inherits Moca.Test.AbstractUnitTest

		Private testContextInstance As TestContext

		Protected myDbms As Dbms
		Protected dba As IDao
		Protected cmd As IDbCommand
		Protected target As SelectCommandWrapper

		'''<summary>
		'''現在のテストの実行についての情報および機能を
		'''提供するテスト コンテキストを取得または設定します。
		'''</summary>
		Public Property TestContext() As TestContext
			Get
				Return testContextInstance
			End Get
			Set(value As TestContext)
				testContextInstance = value
			End Set
		End Property

#Region "追加のテスト属性"
		'
		'テストを作成するときに、次の追加属性を使用することができます:
		'
		'クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
		<ClassInitialize()> _
		Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
			ClassInitialize(testContext)
		End Sub

		'クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
		<ClassCleanup()> _
		Public Shared Sub MyClassCleanup()
			ClassCleanup()
		End Sub

		'各テストを実行する前にコードを実行するには、TestInitialize を使用
		<TestInitialize()> _
		Public Sub MyTestInitialize()
			MyBase.TestInitialize()

			myDbms = New Dbms("SQLContext")
			dba = New DbAccess(myDbms)
			cmd = dba.Connection.CreateCommand()
			target = New SelectCommandWrapper(dba, cmd)
		End Sub

		'各テストを実行した後にコードを実行するには、TestCleanup を使用
		<TestCleanup()> _
		Public Sub MyTestCleanup()
			MyBase.TestCleanup()

			target.Dispose()
			cmd.Dispose()
			dba.Dispose()
		End Sub

#End Region

		'''<summary>
		'''Execute のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ExecuteTest()
			Dim expected As Integer = 2
			Dim actual As Integer

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			actual = target.Execute

			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''ExecuteScalar のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ExecuteScalarTest()
			Dim expected As Integer = 2
			Dim actual As Integer

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT COUNT(*) FROM tbDemoEntities"

			actual = target.ExecuteScalar

			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''Execute のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ExecuteTest1()
			Dim expected As IList(Of tbDemoEntity) = New List(Of tbDemoEntity)
			Dim actual As IList(Of tbDemoEntity)

			expected.Add(New tbDemoEntity)
			expected.Add(New tbDemoEntity)

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			actual = target.Execute(Of tbDemoEntity)()

			Assert.AreEqual(expected.Count, actual.Count)
		End Sub

		'''<summary>
		'''NextResult のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub NextResultTest()
			Dim expected1 As IList(Of tbDemoEntity) = New List(Of tbDemoEntity)
			Dim expected2 As IList(Of tbDemoEntity) = New List(Of tbDemoEntity)
			Dim actual1 As IList(Of tbDemoEntity)
			Dim actual2 As IList(Of tbDemoEntity)
			Dim actual3 As IList(Of tbDemoEntity)

			expected1.Add(New tbDemoEntity)
			expected1.Add(New tbDemoEntity)
			expected2.Add(New tbDemoEntity)

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities; SELECT TOP 1 * FROM tbDemoEntities"

			actual1 = target.Execute(Of tbDemoEntity)()
			actual2 = target.NextResult(Of tbDemoEntity)()
			actual3 = target.NextResult(Of tbDemoEntity)()

			Assert.AreEqual(expected1.Count, actual1.Count)
			Assert.AreEqual(expected2.Count, actual2.Count)
			Assert.IsNull(actual3)
		End Sub

		'''<summary>
		'''Result1stTableEntitis のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub Result1stTableEntitisTest()
			Dim expected() As tbDemoEntity = New tbDemoEntity() {New tbDemoEntity, New tbDemoEntity}
			Dim actual() As tbDemoEntity

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.Result1stTableEntitis(Of tbDemoEntity)()

			Assert.AreEqual(expected.Count, actual.Count)
		End Sub

		'''<summary>
		'''ResultConstantDataSet のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ResultConstantDataSetTest()
			Dim textColumnName As String = "Name"
			Dim valueColumnName As String = "Code"
			Dim blankRow As Boolean = False
			Dim blankValue As Object = Nothing
			Dim delm As String = ":"
			Dim expected As ConstantDataSet = New ConstantDataSet("tbDemoEntities", blankRow, blankValue)
			Dim actual As ConstantDataSet

			expected.Constant.AddRow("hoge1", 10)
			expected.Constant.AddRow("hoge2", 20)

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.ResultConstantDataSet(textColumnName, valueColumnName, blankRow, blankValue, delm)

			Assert.AreEqual(expected.Constant.Count, actual.Constant.Count)
			Assert.AreEqual(expected.Constant(0).Value, actual.Constant(0).Value)
			Assert.AreEqual(expected.Constant(0).Text, actual.Constant(0).Text)
			Assert.AreEqual(expected.Constant(1).Value, actual.Constant(1).Value)
			Assert.AreEqual(expected.Constant(1).Text, actual.Constant(1).Text)
		End Sub

		'''<summary>
		'''ResultConstantDataSet のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ResultConstantDataSetTest1()
			Dim textColumnIndex As Integer = 2
			Dim valueColumnIndex As Integer = 1
			Dim blankRow As Boolean = False
			Dim blankValue As Object = Nothing
			Dim delm As String = ":"
			Dim expected As ConstantDataSet = New ConstantDataSet("tbDemoEntities", blankRow, blankValue)
			Dim actual As ConstantDataSet

			expected.Constant.AddRow("hoge1", 10)
			expected.Constant.AddRow("hoge2", 20)

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.ResultConstantDataSet(textColumnIndex, valueColumnIndex, blankRow, blankValue, delm)

			Assert.AreEqual(expected.Constant.Count, actual.Constant.Count)
			Assert.AreEqual(expected.Constant(0).Value, actual.Constant(0).Value)
			Assert.AreEqual(expected.Constant(0).Text, actual.Constant(0).Text)
			Assert.AreEqual(expected.Constant(1).Value, actual.Constant(1).Value)
			Assert.AreEqual(expected.Constant(1).Text, actual.Constant(1).Text)
		End Sub

		'''<summary>
		'''ResultFirstTableRowEntity のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub Result1stTableEntityTest()
			Dim index As Integer = 1
			Dim expected As tbDemoEntity = New tbDemoEntity
			Dim actual As tbDemoEntity

			expected.Code = 20

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.Result1stTableEntity(Of tbDemoEntity)(index)

			Assert.AreEqual(expected.Code, actual.Code)
		End Sub

		'''<summary>
		'''Result1stTable のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub Result1stTableTest()
			Dim expected As DataTable = New DataTable
			Dim actual As DataTable

			expected.Columns.Add("ID", GetType(String))
			expected.Columns.Add("Code", GetType(Integer))
			expected.Columns.Add("Name", GetType(String))
			expected.Rows.Add("1", 10, "hoge1")

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.Result1stTable

			Assert.AreEqual(expected.Rows(0).Item(1), actual.Rows(0).Item(1))
		End Sub

		'''<summary>
		'''Result1stTableRowEnumerator のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub Result1stTableRowEnumeratorTest()
			Dim dt As DataTable = New DataTable
			Dim expected As IEnumerator(Of DataRow)
			Dim actual As IEnumerator(Of DataRow)

			dt.Columns.Add("ID", GetType(String))
			dt.Columns.Add("Code", GetType(Integer))
			dt.Columns.Add("Name", GetType(String))
			dt.Rows.Add("1", 10, "hoge1")
			expected = dt.Rows.GetEnumerator

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.Result1stTableRowEnumerator

			actual.MoveNext()
			expected.MoveNext()
			Assert.AreEqual(expected.Current.Item(1), actual.Current.Item(1))
		End Sub

		'''<summary>
		'''ResultDataSet のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ResultDataSetTest()
			Dim expected As DataSet = New DataSet
			Dim actual As DataSet

			expected.Tables.Add()
			expected.Tables(0).Columns.Add("ID", GetType(String))
			expected.Tables(0).Columns.Add("Code", GetType(Integer))
			expected.Tables(0).Columns.Add("Name", GetType(String))
			expected.Tables(0).Rows.Add("1", 10, "hoge1")

			target.Command.CommandType = CommandType.Text
			target.CommandText = "SELECT * FROM tbDemoEntities"

			target.Execute()
			actual = target.ResultDataSet

			Assert.AreEqual(expected.Tables(0).Rows(0).Item(1), actual.Tables(0).Rows(0).Item(1))
		End Sub
	End Class

End Namespace
